<?php
/**
 *Users
 * @author tan bing
 * @date 2021-08-10 14:08
 */


namespace Modules\Coupon\Entities;


class Coupon extends BaseModel
{

    /**
     * 与模型关联的表名
     *
     * @var string
     */
    protected $table = 'coupon';

    /**
     * 重定义主键
     *
     * @var string
     */
    protected $primaryKey = 'id';

    /**
     * 指示是否自动维护时间戳
     *
     * @var bool
     */
    public $timestamps = true;

    /**
     * The storage format of the model's date columns.
     *
     * @var string
     */
    protected $dateFormat = 'Y-m-d H:i:s';

    /**
     * 不可批量赋值的属性。
     *
     * @var array
     */
    protected $guarded = [];

    //可搜索字段
    protected $searchField = ['coupon_name', ];

    //可作为条件的字段
    protected $whereField  = ['coupon_type', 'expire_type'];

    // 新增字段
    protected $appends = ['coupon_type_text', 'expire_type_text', 'apply_range_text'];

    /**
     * 获取优惠券类型描述
     * @return string
     * @author tan bing
     * @date 2021-08-13 10:47
     */
    public function getCouponTypeTextAttribute()
    {
        $text = [
            1 => '满减券',
            2 => '折扣券',
        ];

        return $text[$this->coupon_type];
    }

    /**
     * 获取优惠券有效期类型描述
     * @return string
     * @author tan bing
     * @date 2021-08-13 10:47
     */
    public function getExpireTypeTextAttribute()
    {
        $text = [
            1 => '领取之日起',
            2 => '固定时间',
        ];

        return $text[$this->expire_type];
    }

    /**
     * 获取优惠券适用范围描述
     * @return string
     * @author tan bing
     * @date 2021-08-13 10:47
     */
    public function getApplyRangeTextAttribute()
    {
        $text = [
            1 => '全场通用',
            2 => '指定商品',
        ];

        return $text[$this->apply_range];
    }

    /**
     * 获取优惠券领取记录.
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     * @author tan bing
     * @date 2021-08-13 10:56
     */
    public function user_coupon_list()
    {
        return $this->hasMany(UserCoupon::class, 'coupon_id', 'id');
    }

}